package com.ziyun.erp.modules.supp.utils;

import com.alibaba.fastjson.JSONObject;
import com.ziyun.erp.common.utils.HttpUtilsAl;
import com.ziyun.erp.common.utils.ImageUtils;
import io.renren.common.utils.Encodes;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;


/**
 * 营业执照ocr
 *
 * @author gaosheng
 * @date 2018-09-27 10:23
 */
public class OcrUtils {

    /**
     * AppKey：24974030
     * AppSecret：fc09ed4f2e4a80551a576e70d03bfbe4
     * AppCode：d7950b29948c403ca5bfc4a478976a9a
     */
    private static Logger logger = LoggerFactory.getLogger(OcrUtils.class);

    public static JSONObject imageOcr(String companyName, String imageUrl) {
        logger.info("===================company business image ocr start==========================");
        logger.info("===================companyName：" + companyName + "，imageUrl：" + imageUrl);
        String host = "https://dm-58.data.aliyun.com";
        String path = "/rest/160601/ocr/ocr_business_license.json";
        String method = "POST";
        String appcode = "d7950b29948c403ca5bfc4a478976a9a";
        Map<String, String> headers = new HashMap<String, String>();
        // 最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        // 根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/json; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        try {
            // 原图清晰度的60%
            String url = "https://ziyunoss.oss-cn-hangzhou.aliyuncs.com/" + imageUrl + "?x-oss-process=image/quality,q_60";
            byte[] content = ImageUtils.getImageFromNetByUrl(url);
            String encode = new String(Encodes.encodeBase64(content));
            JSONObject bodys = new JSONObject();
            bodys.put("image", encode);
            HttpResponse response = HttpUtilsAl.doPost(host, path, method, headers, querys, bodys.toJSONString());
            logger.info("===================business image ocr response：" + response.getEntity());
            if (response.getStatusLine().getStatusCode() == 200){
                String rs = EntityUtils.toString(response.getEntity());
                logger.info("===================business image ocr result：" + rs);
                JSONObject jsonObjectRs = JSONObject.parseObject(rs);
                logger.info("===================company business image ocr end==========================");
                return jsonObjectRs;
            }
        } catch (Exception e) {
            logger.info("===================business image ocr error：" + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        long timeStart = System.currentTimeMillis();
        imageOcr(null, "erpTest/test/1539063502052.jpg?x-oss-process=image/quality,q_60");
        System.out.println(System.currentTimeMillis()-timeStart);
    }

}
